//
//  SEMyRegistrationViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/22.
//

import UIKit

class SEMyRegistrationViewModel: SEBaseViewModel {
    var registrationList: [SEMyRegistrationModel] = []

    var cancelHandler: ((SEHttpStatus) -> Void)?

    var numberOfSections: Int {
        return registrationList.count
    }

    func registrationModel(indexPath: IndexPath) -> SEMyRegistrationModel {
        return registrationList[indexPath.section]
    }

    var selectedModel = SEMyRegistrationModel()

    /// MARK: - APIRequest

    /// 获取我的号源列表/详情
    func requestMyRegistrationList(id: String = "", flag: Int = 1) {
        SEHttpManager.shared.request(SERegistrationAPI.myRegistrationList(id: id, pageNum: page, pageSize: pageSize, flag: flag), model: SEMyRegistrationListModel.self, withLoading: withLoading) { (model, status) in
            switch status {
            case .success:
                if self.page == 1 {
                    self.registrationList = model?.list ?? []
                } else {
                    self.registrationList.append(contentsOf: model?.list ?? [])
                }
                self.hasMore = self.registrationList.count < model?.total ?? 0
            default:
                self.page == 1 ? (self.page = 1) : (self.page -= 1)
            }
            self.updateData?(status)
        }
    }

    /// 取消预约
    func requestCancelRegistration() {
        SEHttpManager.shared.requestForAny(SERegistrationAPI.registrationCancel(id: selectedModel.sourceId), withLoading: true) { (result, status) in
            self.cancelHandler?(status)
        }
    }
}

